﻿using System.Runtime.InteropServices;
using System.Text;

namespace VoIP.NET.SIP
{
    #region Enums

    public enum ESessionState : int
    {
        SESSION_STATE_NULL,	    /**< Before INVITE is sent or received  */
        SESSION_STATE_CALLING,	    /**< After INVITE is sent		    */
        SESSION_STATE_INCOMING,	    /**< After INVITE is received.	    */
        SESSION_STATE_EARLY,	    /**< After response with To tag.	    */
        SESSION_STATE_CONNECTING,	    /**< After 2xx is sent/received.	    */
        SESSION_STATE_CONFIRMED,	    /**< After ACK is sent/received.	    */
        SESSION_STATE_DISCONNECTED,   /**< Session is terminated.		    */
    }

    #endregion

    #region Delegates

    // callback delegates
    delegate int OnCallStateChanged(int callId, ESessionState stateId);
    delegate int OnCallIncoming(int callId, string number);
    delegate int OnCallHoldConfirm(int callId);
    delegate int OnMessageReceivedCallback(string from, string message);
    delegate int OnBuddyStatusChangedCallback(int buddyId, int status, string statusText);
    delegate int OnRegStateChanged(int accountId, int regState);

    #endregion

    #region Classes

    internal static class SIPCallFunctiions
    {
        #region DLL declarations

#if LINUX
		internal const string PJSIP_DLL = "libpjsipDll.so"; 
#elif MOBILE
		internal const string PJSIP_DLL = "pjsipdll_mobile.dll"; 
#elif TLS
		internal const string PJSIP_DLL = "pjsipdll_tls.dll"; 
#else
        internal const string PJSIP_DLL = "pjsipDll.dll";
#endif

        // call API
        [DllImport(PJSIP_DLL, EntryPoint = "dll_makeCall")]
        private static extern int dll_makeCall(int accountId, string uri);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_releaseCall")]
        private static extern int dll_releaseCall(int callId);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_answerCall")]
        private static extern int dll_answerCall(int callId, int code);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_holdCall")]
        private static extern int dll_holdCall(int callId);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_retrieveCall")]
        private static extern int dll_retrieveCall(int callId);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_xferCall")]
        private static extern int dll_xferCall(int callId, string uri);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_xferCallWithReplaces")]
        private static extern int dll_xferCallWithReplaces(int callId, int dstSession);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_serviceReq")]
        private static extern int dll_serviceReq(int callId, int serviceCode, string destUri);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_dialDtmf")]
        private static extern int dll_dialDtmf(int callId, string digits, int mode);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_getCurrentCodec")]
        private static extern int dll_getCurrentCodec(int callId, StringBuilder codec);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_makeConference")]
        private static extern int dll_makeConference(int callId);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_sendCallMessage")]
        private static extern int dll_sendCallMessage(int callId, string message);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_addBuddy")]
        private static extern int dll_addBuddy(string uri, bool subscribe);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_removeBuddy")]
        private static extern int dll_removeBuddy(int buddyId);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_sendMessage")]
        private static extern int dll_sendMessage(int buddyId, string uri, string message);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_setStatus")]
        private static extern int dll_setStatus(int accId, int presence_state);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_registerAccount")]
        private static extern int dll_registerAccount(string uri, string reguri, string domain, string username, string password, string proxy, bool isdefault);
        [DllImport(PJSIP_DLL, EntryPoint = "dll_removeAccounts")]
        private static extern int dll_removeAccounts();
        [DllImportAttribute(PJSIP_DLL, EntryPoint = "onRegStateCallback")]
        private static extern int onRegStateCallback(OnRegStateChanged cb);

        #endregion

        #region Callback Declarations
        // passing delegates to unmanaged code (.dll)
        [DllImport(PJSIP_DLL)]
        private static extern int onCallStateCallback(OnCallStateChanged cb);
        [DllImport(PJSIP_DLL)]
        private static extern int onCallIncoming(OnCallIncoming cb);
        [DllImport(PJSIP_DLL)]
        private static extern int onCallHoldConfirmCallback(OnCallHoldConfirm cb);
        [DllImport(PJSIP_DLL)]
        private static extern int onMessageReceivedCallback(OnMessageReceivedCallback cb);
        [DllImport(PJSIP_DLL)]
        private static extern int onBuddyStatusChangedCallback(OnBuddyStatusChangedCallback cb);

        #endregion
    }

    #endregion
}
